﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Data;
using DAL.EstRec;

namespace BAL.EstRec
{
    public class SincronizarArchivosFDV
    {
        private List<string> UNIDS_Servidor;
        private List<string> UNIDS_Locales;
        private AppConfs ac = new AppConfs();
        public bool atLeastOneUpdated;

        public SincronizarArchivosFDV() {
            this.atLeastOneUpdated = false;
            UNIDS_Servidor = new List<string>();
            UNIDS_Locales = new List<string>();

            getUnidsServidor();
            getUnidsLocales();
            JoinUNIDS();
        }

        private void getUnidsServidor() {
                   
            foreach (string item in Directory.GetFiles(ac.cnf.CierreConsolidacionServer))
            {
                string aux = item.Split('\\').Last().ToString();
                aux = aux.Substring(0, aux.Length - 4);
                UNIDS_Servidor.Add(aux);
            }
        }

        private void getUnidsLocales() { 
        
            DataTable dt = new DataTable();
            dt = SrvDB.ExecuteQuery("EXEC SP_GET_NULLS_UNIDS");

            for (int i = 0; i < dt.Rows.Count; i++) {

                UNIDS_Locales.Add(dt.Rows[i][0].ToString());
            }
        }

        private void JoinUNIDS() {

            //actualizamos
            var listServer = UNIDS_Servidor;
            var listLocal = UNIDS_Locales;


            var result = from server in listServer
                         join local in listLocal on server equals local
                         select server;
            foreach(object o in result){
                //System.Windows.MessageBox.Show(o.ToString());
                try
                {
                    SrvDB.ExecuteNonQuery("EXEC SP_FDV_CHANGE_DATE @VERSION = " + o.ToString());
                    this.atLeastOneUpdated = true;
                }
                catch (Exception ex)
                {
                    ;
                }
            }
        }
    }
}
